<!DOCTYPE html>
<html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" th:value= "${id} " id="id"  name="id" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择用户</label>
        <div class="layui-input-inline">
            <select id="service" name="service" lay-verify="required"  class="select">
                <option value="0">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">选择书籍</label>
        <div class="layui-input-inline">
            <select id="bookSelect" name="bookSelect" lay-verify="required"  class="select">
                <option value="0">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">借阅状态</label>
        <div class="layui-input-block">
            <input type="text" name="status" id="status" style="width: 28.5%;" placeholder="请输入借阅状态" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-block">
            <input type="text" name="createTime" id="createTime" style="width: 28.5%;" placeholder="请输入创建时间" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否删除</label>
        <div class="layui-input-block">
            <input type="text" name="deleted" id="deleted" style="width: 28.5%;" placeholder="请输入是否删除" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var form = layui.form;
    var element = layui.element;

    window.onload= function (){
        var id = $("#id").val()
        if(id != null  && id != ""){
            CoreUtil.sendGet('/userBook/info?id='+id,null,function (res) {
                //初始化渲染数据
                console.log(res)
                var params={
                    "page":1,
                    "limit":9999
                };
                CoreUtil.sendPost("/sys/users",params,function (resa){
                    if (resa.data.records!= null) {
                        $("#service").empty();
                        $("#service").html("<option value='0'>请选择</option>");
                        $.each(resa.data.records, function(index, item) {
                            if(res.data.userId !=null){
                                var option = new Option(item.nickName,item.id);
                                if(res.data.userId == item.id){
                                    option.setAttribute("selected",'true');
                                }
                            }else{
                                var option = new Option(item.nickName,item.id);
                            }
                            $('#service').append(option);//往下拉菜单里添加元素
                        })
                    } else {
                        $("#service").append(new Option("暂无数据", ""));
                    }
                    //重新渲染
                    form.render("select");
                })

                CoreUtil.sendGet("/book/selectByUser?bookId="+res.data.bookId+"&type="+res.data.status,params,function (bookRes){
                    if (bookRes.data != null){
                        $("#bookSelect").empty();
                        $("#bookSelect").html("<option value='0'>请选择</option>");
                        $.each(bookRes.data, function(index, item) {
                            if(res.data.bookId !=null){
                                var option = new Option(item.bookName,item.id);
                                if(res.data.bookId == item.id){
                                    option.setAttribute("selected",'true');
                                }
                            }else{
                                var option = new Option(item.bookName,item.id);
                            }
                            $('#bookSelect').append(option);//往下拉菜单里添加元素
                        })
                    } else {
                        $("#bookSelect").append(new Option("暂无数据", ""));
                    }
                    //重新渲染
                    form.render("select");
                })

                if(res.data.status ===1){
                    $("#status").val("正在借阅")
                }else {
                    $("#status").val("已归还")
                }
    ``
                document.getElementById("createTime").value=res.data.createTime;
                if(res.data.deleted ===1){
                    $("#deleted").val("正常")
                }else {
                   $("#deleted").val("已删除")
                }
            })
        }else{
            var params={
                "page":1,
                "limit":9999
            };
            CoreUtil.sendPost("/sys/users",params,function (resa){
                if (resa.data.records!= null) {
                    $("#service").empty();
                    $("#service").html("<option value='0'>请选择</option>");
                    $.each(resa.data.records, function(index, item) {
                        var option = new Option(item.nickName,item.id);
                        $('#service').append(option);//往下拉菜单里添加元素
                    })
                } else {
                    $("#service").append(new Option("暂无数据", ""));
                }
                //重新渲染
                form.render("select");
            })

            CoreUtil.sendGet("/book/selectByUser?bookId=null&type=0",params,function (bookRes){
                if (bookRes.data != null){
                    $("#bookSelect").empty();
                    $("#bookSelect").html("<option value='0'>请选择</option>");
                    $.each(bookRes.data, function(index, item) {
                        var option = new Option(item.bookName,item.id);
                        $('#bookSelect').append(option);//往下拉菜单里添加元素
                    })
                } else {
                    $("#bookSelect").append(new Option("暂无数据", ""));
                }
            })
        }

    };
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            }, function () {

                // 关闭弹出层
                layer.close(index);

                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);

            });

            return false;
        });
    });
</script>
</body>
</html>